<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\Export\MemberExchangeExport;
use App\Http\Controllers\Admin\Business\Repository\Combo;
use App\Member;
use App\Models\ComboExchange;
use App\Models\MemberExchange;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class MemberExchangeController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('兑换列表');
            $content->description('兑换列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(MemberExchange::class, function (Grid $grid) {
          /*  $grid->model()->join('members','members.id','=','member_exchange.member_id')
                ->join('combo_exchange','combo_exchange.id','=','member_exchange.exchange_id')
                ->select('member_exchange.*','members.nickname','combo_exchange.code','combo_exchange.combo_id');*/
            $grid->id('ID')->sortable();
            $grid->member_id('用户昵称')->display(function($value){
               return  Member::find($value)->nickname ?? '未知';
            });
            $grid->id_number('身份证号')->display(function ($value){
                return Member::find($this->member_id)->id_number ?? '';
            });
            $grid->truename('真实姓名')->display(function ($value){
                return Member::find($this->member_id)->truename ?? '';
            });
            $grid->exchange_id('套餐')->display(function($value){
                    $comboExchange=  ComboExchange::find($value) ;
                    $combo= Combo::find($comboExchange->combo_id);
                    return $combo->title;
            });
            $grid->exporter(new MemberExchangeExport());
            $grid->code('激活码')->display(function($value){

                $comboExchange=  ComboExchange::find($this->exchange_id) ;
                return $comboExchange->code;
            });

            $grid->created_at('领取时间');
            $grid->disableActions();
            $grid->disableCreateButton();
            $grid->filter(function(Grid\Filter $filter){
                $filter->like('nickname','用户昵称');
                $filter->between('created_at', '生成时间')->datetime();
            });
            $grid->tools(function (Grid\Tools $tools) {

                $tools->batch(function ($batch) {
                    $batch->disableDelete();
                });
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(MemberExchange::class, function (Form $form) {

            $form->display('id', 'ID');

            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
}
